package com.nextStep.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nextStep.entity.Users;
import com.nextStep.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    //增加一个用户
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public @ResponseBody  String addUser(@RequestBody Map<String, String> map,HttpServletRequest request){
        Users users=new Users();
        users.setName(map.get("name"));
        users.setEmail(map.get("email"));
        users.setPassword(map.get("password"));
        String emailVeri=map.get("emailVeri");
            //状态
        String state= userService.addUser(users,emailVeri,request);
        String retu="";
        ObjectMapper mapper = new ObjectMapper();
        try {
            retu = mapper.writeValueAsString(state);  //转换为键值对格式   匹配json
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return  retu;
    }

    //发送验证码
    @RequestMapping(value = "/sendUpdatePasswordVeriEmail",method = RequestMethod.POST)
    public @ResponseBody String sendUpdatePasswordVeriEmail(@RequestBody String userEmail,HttpServletRequest request){
        Boolean sendStart = userService.sendUpdatePasswordVeriEmail(userEmail,request);
        ObjectMapper mapper = new ObjectMapper();
        String state = "error";
        if(sendStart){
            state= "success";
        }else {
            state= "error";
        }
        String retu="";
        try {
            retu = mapper.writeValueAsString(state);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retu;

    }


    //跳转到输入修改密码验证页面
    @RequestMapping("/updatePasswordVeriInput")
    public  String updatePasswordVeriInput() {
        return "updatePasswordVeriInput";
    }

    //跳转到修改密码页面
    @RequestMapping("/updatePasswordAfter")
    public String updatePasswordAfter() {
        return "updatePasswordAfter";
    }



    //验证修改密码的验证码
    @RequestMapping(value = "/updatePasswordVeriEnter" ,method = RequestMethod.POST)
    public @ResponseBody String updatePasswordVeriEnter(@RequestBody String veriEnter, HttpServletRequest request){
        Boolean start = userService.updatePasswordVeriEnter(veriEnter,request);
        ObjectMapper mapper=new ObjectMapper();
        String state="error";
        if(start){
            state= "success";
        }else{
            state= "error";
        }
        String retu ="";
        try {
            retu = mapper.writeValueAsString(state);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retu;

    }

    //修改密码
    @RequestMapping("/updatePasswordEnter")
    public @ResponseBody String updatePasswordEnter(@RequestBody String password,HttpServletRequest request){
        Boolean state = userService.updatePasswordEnter(password,request);
        String retu="";
        String retuTemp="";
        if(state){
            retuTemp = "success";
        }else {
            retuTemp = "error";
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            retu = mapper.writeValueAsString(retuTemp);  //转换为键值对格式   匹配json
        }catch (JsonProcessingException e){
        }
        return retu;


    }

    //更新一个用户
    @RequestMapping("/update")
    public String updateUser(Users users){
        return null;

    }

    //查询一个用户
    @RequestMapping("/queryById")
    public String queryUserById(int id){
        return null;

    }

    //对比一个用户(用户名密码）
    @RequestMapping(value = "/equale" ,method = RequestMethod.POST)
    public @ResponseBody String  equaleUser(@RequestBody Users user,HttpServletRequest request)   {

        String state="";    //状态
        int count = userService.equaleUser(user);
        if(count == 1){
            state="success";
            Map map = new HashMap();
            map.put("name",user.getName());
            Users userComp = userService.queryUser(map);
            request.getSession().setAttribute("LOGIN_DATA",userComp);
        }else {
            state="error";
        }
        String retu="";
        ObjectMapper mapper = new ObjectMapper();
        try {
            retu = mapper.writeValueAsString(state);  //转换为键值对格式   匹配json
        }catch (JsonProcessingException e){
        }
        return retu;
    }

    //注册用户验证邮箱，发送验证码
    @RequestMapping(value = "/sendRegisterEmailVeri",method = RequestMethod.POST)
    public @ResponseBody String sendRegisterEmailVeri(@RequestBody String email,HttpServletRequest request)  {
        boolean sendState = false;
        try {
            sendState = userService.sendRegisterEmailVeri(email,request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String state="";
        if(sendState){
            state="success";
        }else {
            state="error";
        }
        ObjectMapper mapper = new ObjectMapper();
        String retu ="";
        try {
            retu = mapper.writeValueAsString(state);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return retu;
    }

    //退出
    @RequestMapping("/exit")
    public String exit(HttpServletRequest request){
        request.getSession().removeAttribute("LOGIN_DATA");
        return "index";
    }

}
